package com.ruoyi.monitormanage.controller;

import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictVO;
import com.ruoyi.common.core.feign.ModulesSystemFeign;
import com.ruoyi.monitormanage.domain.TTestpoint;
import com.ruoyi.monitormanage.mapper.TTestpointMapper;
import com.ruoyi.monitormanage.service.ITTestpointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.monitormanage.domain.TCreategps;
import com.ruoyi.monitormanage.service.ITCreategpsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 填入gpsController
 * 
 * @author ruoyi
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/creategps")
public class TCreategpsController extends BaseController
{
    @Autowired
    private ITCreategpsService tCreategpsService;
    @Autowired
    private ITTestpointService tTestpointService;
    @Autowired
    private TTestpointMapper tTestpointMapper;
    @Autowired
    public ModulesSystemFeign systemFeign;
    /**
     * 查询填入gps列表
     */
    //@RequiresPermissions("creategps:creategps:list")
    @GetMapping("/list")
    public TableDataInfo list(TCreategps tCreategps)
    {
        startPage();
        List<TCreategps> list = tCreategpsService.selectTCreategpsList(tCreategps);
        return getDataTable(list);
    }

    /**
     * 导出填入gps列表
     */
    @RequiresPermissions("creategps:creategps:export")
    @Log(title = "填入gps", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCreategps tCreategps)
    {
        List<TCreategps> list = tCreategpsService.selectTCreategpsList(tCreategps);
        ExcelUtil<TCreategps> util = new ExcelUtil<TCreategps>(TCreategps.class);
        util.exportExcel(response, list, "填入gps数据");
    }

    /**
     * 获取填入gps详细信息
     */
    @RequiresPermissions("creategps:creategps:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tCreategpsService.selectTCreategpsById(id));
    }

    /**
     * 新增填入gps
     */
    @Log(title = "填入gps", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCreategps tCreategps)
    {
        /*String currentYear = Integer.toString(Year.now().getValue());
        String currentDateTime = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String twoHoursLater = LocalDateTime.now()
                .plusHours(2)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        tCreategps.setYear(currentYear);
        tCreategps.setCreatetime(currentDateTime);
        tCreategps.setEffectivetime(twoHoursLater);*/
        settcreategps(tCreategps);
        TTestpoint tTestpoint = new TTestpoint();
        tTestpoint.setYear(tCreategps.getYear());
        tTestpoint.setOrgId(tCreategps.getDeptid());
        if (!tTestpointService.checkTestpointUnique(tTestpoint))
        {
            if (!(tCreategpsService.checkCreategpsUnique(tCreategps))){
                //return error("新增监测点gps失败，该监测点gps已存在");
                edit(tCreategps);
                //return toAjax(tCreategpsService.insertTCreategps(tCreategps));
                //
            }else {
                return toAjax(tCreategpsService.insertTCreategps(tCreategps));
                //edit(tCreategps);
            }
        }else {
            return error("无监测点信息，请先添加监测点");
        }
        return success("添加成功");
    }
    public TCreategps settcreategps(@RequestBody TCreategps tCreategps){
        String currentYear = Integer.toString(Year.now().getValue());
        String currentDateTime = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        SysDictVO dictVO = systemFeign.getSysDict("duration");
        String twoHoursLater = LocalDateTime.now()
                .plusHours(Long.parseLong(dictVO.getValue()))
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        tCreategps.setYear(currentYear);
        tCreategps.setCreatetime(currentDateTime);
        tCreategps.setEffectivetime(twoHoursLater);
        return tCreategps;
    }
    /**
     * 修改填入gps
     */
    /*@Log(title = "填入gps", businessType = BusinessType.UPDATE)
    @PutMapping
    */public AjaxResult edit(@RequestBody TCreategps tCreategps)
    {
        //修改监测点gps之后要改变监测点表的status的状态从1改为0
        if (tCreategpsService.updateTCreategps(tCreategps) > 0){
            /*TTestpoint testpoint = tTestpointMapper.getTestpoint(tCreategps.getYear(), tCreategps.getDeptid());
            testpoint.setStatus("1");
            if (tTestpointMapper.updateTTestpoint(testpoint) > 0){
                return AjaxResult.success("更新成功");
            }*/
            return AjaxResult.success("更新成功");
        }
        return AjaxResult.error("更新失败，请联系管理员");
    }

    /**
     * 删除填入gps
     */
    @RequiresPermissions("creategps:creategps:remove")
    @Log(title = "填入gps", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCreategpsService.deleteTCreategpsByIds(ids));
    }
}
